package com.loto.webapp.action.manage;

import java.util.ArrayList;
import java.util.List;

import org.apache.struts2.ServletActionContext;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

import com.loto.webapp.common.BaseAction;
import com.loto.webapp.common.PageResultSet;
import com.loto.webapp.dao.UserDao;
import com.loto.webapp.model.TopMenu;
import com.loto.webapp.model.User;
import com.loto.webapp.util.Files;

public class UserAccessAction extends BaseAction {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2505337923051007173L;

	private String id;

	private List<TopMenu> menuList;

	private List<TopMenu> childMenuList;

	private List<TopMenu> userMenuList;

	public String execute() {
		loadMenu();
		return SUCCESS;
	}

	private String loadMenu() {
		try {
			Document document = Files.getBaseMenuXml(ServletActionContext
					.getRequest());
			org.w3c.dom.Element rootElement = document.getDocumentElement();
			NodeList list = rootElement.getElementsByTagName("Menu");
			this.menuList = new ArrayList<TopMenu>();
			this.childMenuList = new ArrayList<TopMenu>();
			for (int i = 0; i < list.getLength(); i++) {
				org.w3c.dom.Element element = (org.w3c.dom.Element) list
						.item(i);
				TopMenu menu = new TopMenu();
				menu.setId(element.getAttribute("ID"));
				menu.setName(element.getAttribute("Name"));
				menu.setLink(element.getAttribute("Url"));
				menuList.add(menu);
				NodeList sublist = element.getElementsByTagName("ChildMenu");
				for (int j = 0; j < sublist.getLength(); j++) {
					org.w3c.dom.Element subelement = (org.w3c.dom.Element) sublist
							.item(j);
					TopMenu childMenu = new TopMenu();
					childMenu.setId(subelement.getAttribute("ID"));
					childMenu.setName(subelement.getAttribute("Name"));
					childMenu.setLink(subelement.getAttribute("Url"));
					childMenu.setParentId(element.getAttribute("ID"));
					childMenuList.add(childMenu);
				}
			}

			document = Files.getMenuXml(ServletActionContext.getRequest(), id);
			if (document != null) {
				rootElement = document.getDocumentElement();
				list = rootElement.getElementsByTagName("ChildMenu");
				if (list.getLength() > 0) {
					this.userMenuList = new ArrayList<TopMenu>();
					for (int i = 0; i < list.getLength(); i++) {
						org.w3c.dom.Element element = (org.w3c.dom.Element) list
								.item(i);
						TopMenu menu = new TopMenu();
						menu.setId(element.getAttribute("ID"));
						menu.setName(element.getAttribute("Name"));
						menu.setLink(element.getAttribute("Url"));
						userMenuList.add(menu);
					}
				}
			}
			return SUCCESS;
		} catch (Exception e) {
			return ERROR;
		}
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public List<TopMenu> getMenuList() {
		return menuList;
	}

	public void setMenuList(List<TopMenu> menuList) {
		this.menuList = menuList;
	}

	public List<TopMenu> getChildMenuList() {
		return childMenuList;
	}

	public void setChildMenuList(List<TopMenu> childMenuList) {
		this.childMenuList = childMenuList;
	}

	public List<TopMenu> getUserMenuList() {
		return userMenuList;
	}

	public void setUserMenuList(List<TopMenu> userMenuList) {
		this.userMenuList = userMenuList;
	}

}
